﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace DinningWeb.DAO
{
    /// <summary>
    /// 用户信息实体类
    /// </summary>
    [Table("user_info")]
    public class UserInfo
    {
        [Key]
        [Required]
        [StringLength(10)]
        [Column("user_id")]
        [Display(Name = "工号")]
        public string UserId { get; set; }

        [StringLength(50)]
        [Display(Name = "姓名")]
        [Column("user_name")]
        public string? UserName { get; set; }

        [StringLength(4)]
        [Column("user_gent")]
        [Display(Name = "性别")]
        public string? UserGender { get; set; }

        [StringLength(60)]
        [Column("user_wxid")]
        [Display(Name = "微信ID")]
        public string? UserWxId { get; set; }

        [StringLength(100)]
        [Column("user_depart")]
        [Display(Name = "部门")]
        public string? UserDepartment { get; set; }

        [StringLength(15)]
        [Column("user_tel")]
        [Display(Name = "电话")]
        public string? UserTel { get; set; }

        [StringLength(10)]
        [Column("user_type")]

        [Display(Name = "类型")]
        public string? UserType { get; set; }

        [StringLength(50)]
        [Column("user_remark")]
        [Display(Name = "备注")]
        public string? UserRemark { get; set; }

        [Column("lastlogin_time")]
        [Display(Name = "上次登录实践")]
        public DateTime? LastLoginTime { get; set; }

        [Column("userpwd_encrypted", TypeName = "varbinary(256)")]
        public byte[]? UserPasswordEncrypted { get; set; }

        [StringLength(50)]
        [Column("user_email")]
        [Display(Name = "电子邮箱")]
        public string? UserEmail { get; set; }

        [Column("user_state")]
        [Display(Name = "用户状态")]
        public int UserState { get; set; } = 1;

        /// <summary>
        /// 验证用户密码
        /// </summary>
        public bool VerifyPassword(string inputPassword)
        {
            // 实际密码验证逻辑
            return true;
        }

        /// <summary>
        /// 更新最后登录时间
        /// </summary>
        public void UpdateLastLogin()
        {
            LastLoginTime = DateTime.Now;
        }

        /// <summary>
        /// 计算某天的剩余补贴余额
        /// </summary>
        public decimal GetDailyRemainingSubsidy(DateTime date, List<DiningInfo> diningRecords, decimal dailySubsidyLimit)
        {
            var dailyConsumption = diningRecords
                .Where(d => d.UserId == UserId &&
                           d.DiningTime.Date == date.Date)
                .Sum(d => d.DiningPrice - d.DiningSelfcost);

            return dailySubsidyLimit - dailyConsumption>0? (decimal)(dailySubsidyLimit - dailyConsumption):0;
        }

        /// <summary>
        /// 获取指定日期区间的总补贴金额
        /// </summary>
        public decimal GetSubsidyTotal(DateTime startDate, DateTime endDate, List<DiningInfo> diningRecords)
        {
            return (decimal)diningRecords .Where(d => d.UserId == UserId &&
                           d.DiningTime >= startDate &&
                           d.DiningTime <= endDate)
                .Sum(d => d.DiningPrice - d.DiningSelfcost);
        }
    }
}
